<template>
  <c-dialog ref="dialog" :data="selData" single title="异常说明" @open="openFun">
    <c-list ref="form" :model="formData" :rules="formRules" label-width="120px">
      <c-list-item :span="2" label="门店建设项目号" prop="constCode">
        {{ formData.constCode }}
      </c-list-item>
      <c-list-item :span="2" label="门店" prop="shopName">
        {{ formData.shopName }}
      </c-list-item>
      <c-list-item label="异常类型" prop="exceptionName">
        <el-select
          v-model="formData.exceptionName"
          placeholder="请选择"
          style="width: 100%"
        >
          <el-option
            v-for="item in exceptionOptions"
            :key="item.dictCode"
            :label="item.dictName"
            :value="item.dictName"
          />
        </el-select>
      </c-list-item>
      <c-list-item label="异常说明" prop="exceptionDesc">
        <el-input v-model="formData.exceptionDesc" maxlength="1024" type="textarea" show-word-limit :autosize="{minRows:3}" placeholder="异常说明" clearable />
      </c-list-item>
      <c-list-item>
        <div class="btn-container">
          <c-button @click="cancelHandle">取消</c-button>
          <c-button type="primary" @click="confirmHandle">确定</c-button>
        </div>
      </c-list-item>
    </c-list>
  </c-dialog>
</template>
<script>
import { saveAccept } from '@/api/check'
import { getDictByPatchType } from '@/api/dataDict'

export default {
  name: 'Exception',
  props: {
    selData: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      formData: {
        id: '',
        constCode: '',
        shopName: '',
        exceptionName: '',
        exceptionDesc: ''
      },
      exceptionOptions: [], // EXCEPTION_TYPE
      formRules: {
        exceptionName: [{ required: true, message: '请选择', trigger: 'blur' }]
      }
    }
  },
  computed: {
    detail() {
      return this.selData.length > 0 ? this.selData[0] : {}
    }
  },
  methods: {
    async openFun() {
      if (!this.selData || this.selData.length === 0) {
        return
      }
      const { EXCEPTION_TYPE } = await getDictByPatchType(['EXCEPTION_TYPE'])
      this.exceptionOptions = EXCEPTION_TYPE
      const { id, constCode, shopName, exceptionName, exceptionDesc } = this.selData[0]
      this.formData = { id, constCode, shopName, exceptionName, exceptionDesc }
    },
    confirmHandle() {
      return this.$refs.form.validate().then(() => {
        return saveAccept(this.formData).then(res => {
          this.$message.success('操作成功！')
          this.$emit('on-success')
          this.$refs.dialog.visible = false
        })
      })
    },
    cancelHandle() {
      this.$refs.dialog.visible = false
    }
  }
}
</script>
